<?php


namespace Tbf\Component\Crypto\Tests;


use Tbf\Bundle\TestBundle\TestSuite\TbfTestCase;
use Tbf\Component\Arrays\BitBooleanArray;
use Tbf\Component\Crypto\LinearCongruentialGenerator\LcgIndexGenerator;

class LcgIndexGeneratorTest extends TbfTestCase{
    function test1(){
        $this->checkLcgParameter(0,8,5,2531011);
        $this->checkLcgParameter(100000,899998,81819,156785);
    }
    function checkLcgParameter($s,$m,$a,$c){
        $e = $s+$m;
        $obj = new LcgIndexGenerator($s,$m,$a,$c);
        $has_out_array = new BitBooleanArray($m);
        for($i=0;$i<$m;$i++){
            $ret = $obj->generate($i);
            if ($ret<$s){
                $this->fail(sprintf('get a number before start ,i: %d, ret: %d',$i,$ret));
            }
            if ($ret>=$e){
                $this->fail(sprintf('get a number after end ,i: %d, ret: %d',$i,$ret));
            }
            if ($has_out_array->get($ret-$s)){
                $this->fail(sprintf('get a repeat number ,i: %d, ret: %d',$i,$ret));
            }
            $has_out_array->set($ret-$s,true);
        }
        $ret = $obj->generate($m);
        $this->assertTrue($has_out_array->get($ret-$s));
    }
}